﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SilentVoice.Models;
using System.Data.Entity.Validation;

namespace SilentVoice.Data.Tests
{
    public class TestData
    {
        public static Account Nobles;
        public static Role AccountOwnerRole;
        public static Role UserRole;
        public static Group FourthGradeGroup;
        public static User MaryFaculty;
        public static User JoeUser;
        public static Event HistoryForum;
        public static ScratchPad MyJournal;

        public static void Create()
        {
            using (NotesContext context = new NotesContext())
            {
                

                if (context.Database.Exists()) { context.Database.Delete(); }

                context.Database.CreateIfNotExists();

                try
                {
                    Nobles = new Account()
                    {
                        Name = "Nobles and Greenough"
                    };
                    Nobles = context.Accounts.Add(Nobles);
                    AccountOwnerRole = new Role()
                    {
                        Name = Role.RoleTypes.AccountOwner
                    };

                    UserRole = new Role()
                    {
                        Name = Role.RoleTypes.User
                    };
                    AccountOwnerRole = context.Roles.Add(AccountOwnerRole);
                    UserRole = context.Roles.Add(UserRole);
                    context.SaveChanges();
                    FourthGradeGroup = new Group()
                    {
                        AccountId = Nobles.Id,
                        Name = "4th Grade"
                    };
                    Group FifthGradeGroup = new Group()
                    {
                        AccountId = Nobles.Id,
                        Name = "5th Grade"
                    };
                    FourthGradeGroup = context.Groups.Add(FourthGradeGroup);
                    FifthGradeGroup = context.Groups.Add(FifthGradeGroup);
                    context.SaveChanges();
                    MaryFaculty = new User()
                    {
                        AccountId = Nobles.Id,
                        FirstName = "Mary",
                        LastName = "Faculty",
                        Designation = "Faculty",
                        Email = "Mary.Faculty@cox.com",
                        PasswordSalt = "123456",
                        PasswordHash= "124556",
                        RoleId = UserRole.Id
                    };

                    JoeUser = new User()
                    {
                        AccountId = Nobles.Id,
                        FirstName = "Joe",
                        LastName = "User",
                        Designation = "4th Grader",
                        Email = "Joe.User@aol.com",
                        PasswordSalt = "123456",
                        PasswordHash = "124556",
                        RoleId = UserRole.Id
                    };
                    MaryFaculty = context.Users.Add(MaryFaculty);
                    JoeUser = context.Users.Add(JoeUser);
                    context.SaveChanges();
                    HistoryForum = new Event()
                    {
                        Name = "History Forum",
                        AccountId = Nobles.Id,
                        Description = "Reviewing the upcoming presidential election",
                        Outline = "...",
                        AllowedGroups = new List<Group>() { FourthGradeGroup },
                        OwnerId = MaryFaculty.Id
                    };
                    HistoryForum = context.Events.Add(HistoryForum);
                    context.SaveChanges();
                    MyJournal = new ScratchPad()
                    {
                        EventId = HistoryForum.Id,
                        OwnerId = JoeUser.Id,
                        Notes = "Some of my notes so far"
                    };

                    MyJournal = context.ScratchPads.Add(MyJournal);

                    FourthGradeGroup.Users.Add(JoeUser);

                    context.SaveChanges();
                }
                catch (DbEntityValidationException validationErrors)
                {
                    validationErrors.EntityValidationErrors
                        .ToList().ForEach(x => x.ValidationErrors.ToList()
                            .ForEach(y=>System.Console.WriteLine("{0} - {1}", y.PropertyName, y.ErrorMessage)));
                    
                    throw validationErrors;
                }
            }
        }

        public static void Wipe()
        {
            using (NotesContext context = new NotesContext())
            {
                //context.Database.Delete();
            }
        }

        
        


        
    }
}
